package com.example;

import org.openjdk.jol.info.ClassLayout;

/**
 * 演示 无锁 到 偏向锁
 */
public class BiaseLockDemo {

   static final Person person = new Person();

    public static void main(String[] args) throws InterruptedException {
        //需要开启偏向锁 默认是关闭
        // -XX:+UseBiasedLocking    -XX:+PrintFlagsFinal 打印jvm运行时参数，验证参数修改-XX:BiasedLockingStartupDelay=0
        //-XX:+PrintFlagsFinal 打印jvm运行时参数，验证参数修改

        System.out.println(Thread.currentThread().getName()+":无锁 --------------------start");

        System.out.println(Thread.currentThread().getName()+":hashCode(16进制):"+Integer.toHexString(person.hashCode()));
        System.out.println(ClassLayout.parseInstance(person).toPrintable());
        System.out.println(Thread.currentThread().getName()+":无锁 --------------------end");


                synchronized (person) {
                    System.out.println(Thread.currentThread().getName() + ": 偏向锁 --------------------start");
                    System.out.println(Thread.currentThread().getName() + ": +hashCode(16进制):" + Integer.toHexString(person.hashCode()));
                    System.out.println(ClassLayout.parseInstance(person).toPrintable());
                    System.out.println(Thread.currentThread().getName() + ":偏向锁 --------------------start");
                }




    }



}
